#include "logindialog.h"
#include "ui_logindialog.h"
#include "mainwindow.h"
#include <QMessageBox>
#include "userdao.h"
loginDialog::loginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::loginDialog)
{
    ui->setupUi(this);
}

loginDialog::~loginDialog()
{
    delete ui;
}

void loginDialog::on_pushButton_title_clicked()//登录
{
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_password->text();

    bool ok;
    UserEnity ue;
    ue.username = username;
    ue.password = password;
    UserDao::getInstance()->selectUser(ue,ok);

    if(username == "1" && password == "1")
    {
        MainWindow *mw = new MainWindow();//创建堆上，生命周期长
        mw->show();
        close();
    }
    else
    {
        QMessageBox::information(this,"提示","用户名或密码错误");
    }
}

void loginDialog::on_pushButton_register_clicked()
{
    QString username = ui->lineEdit_username->text();//获取用户名
    QString password = ui->lineEdit_password->text();//获取密码

    if(username == "" || password == "")
    {
        QMessageBox::warning(this,"警告","用户名或密码不能为空");
        return;
    }

    UserEnity ue;
    ue.username = username;//让UserEnity结构体中的用户名等于当前用户输入的用户名
    ue.password = password;//让UserEnity结构体中的密码等于当前用户输入的密码
    bool ok;
    UserDao::getInstance()->insertUser(ue,ok);
    if(ok)
    {
        QMessageBox::information(this,"提示","注册成功");
        return;
    }
    else
    {
        QMessageBox::warning(this,"警告","注册失败");
        return;
    }

}
